package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.CaptchaQuery;
import cn.org.eshow.bean.query.WithdrawAccountQuery;
import cn.org.eshow.common.CommonVar;
import cn.org.eshow.model.Captcha;
import cn.org.eshow.model.User;
import cn.org.eshow.model.WithdrawAccount;
import cn.org.eshow.service.CaptchaManager;
import cn.org.eshow.service.WithdrawAccountManager;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 提现账号API接口类
 */
@AllowedMethods({"mine", "update"})
public class WithdrawAccountAction extends ApiBaseAction<WithdrawAccount> {

    private static final long serialVersionUID = 1L;

    @Autowired
    private WithdrawAccountManager withdrawAccountManager;
    @Autowired
    private CaptchaManager captchaManager;

    private WithdrawAccountQuery query = new WithdrawAccountQuery();
    private WithdrawAccount withdrawAccount = new WithdrawAccount();
    private List<WithdrawAccount> withdrawAccounts;

    private String code;//验证码

    /**
     * 获取我的提现账号信息
     */
    public void mine() {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        query.setUserId(user.getId());
        WithdrawAccount withdrawAccount = withdrawAccountManager.check(query);
        if (withdrawAccount == null) {
            RenderUtil.failure("暂无数据");
            return;
        }
        RenderUtil.success("获取成功", withdrawAccount);
    }

    /**
     * 更新提现账号信息
     */
    public void update() {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        Captcha captcha = captchaManager.updateState(captchaManager.check(new CaptchaQuery(code, user.getUsername(), CommonVar.CODE_UNUSED)));
        if (captcha == null) {
            RenderUtil.failure("验证码错误");
            return;
        }

        WithdrawAccount old = withdrawAccountManager.mine(user);//获取我的提现账号信息
        withdrawAccount = withdrawAccountManager.update(old, withdrawAccount);//更新提现账号信息

        RenderUtil.success("保存成功");
    }

    public WithdrawAccountQuery getQuery() {
        return query;
    }

    public void setQuery(WithdrawAccountQuery query) {
        this.query = query;
    }

    public WithdrawAccount getWithdrawAccount() {
        return withdrawAccount;
    }

    public void setWithdrawAccount(WithdrawAccount withdrawAccount) {
        this.withdrawAccount = withdrawAccount;
    }

    public List<WithdrawAccount> getWithdrawAccounts() {
        return withdrawAccounts;
    }

    public void setWithdrawAccounts(List<WithdrawAccount> withdrawAccounts) {
        this.withdrawAccounts = withdrawAccounts;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}
